DAY36:Maximum subarray sum


Posted by birdbirdmurmur on 2023-08-18

題目連結

https://www.codewars.com/kata/54521e9ec8e60bc4de000d6c

解法

function maxSequence(arr) {
  let maxSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let currentSum = 0;
    for (let j = i; j < arr.length; j++) {
      currentSum += arr[j];
      maxSum = Math.max(maxSum, currentSum);
    }
  }
  return maxSum;
}

筆記

使用雙重迴圈
比較當前sum和maxSum
最後回傳maxSum

[-2, 1, -3, 4, -1, 2, 1, -5, 4]


#javascript #Codewars







Related Posts

學習 Git (3) - 開始版控的最後一步:Commit

學習 Git (3) - 開始版控的最後一步:Commit

text JS屬性 語法

text JS屬性 語法

docker 是什麼:快速部署應用程式的實用工具

docker 是什麼:快速部署應用程式的實用工具


Comments